<?php

namespace app\admin\logic;

use app\common\model\Glogs;
use app\common\model\Goods;
use app\common\model\Tgoods;
use think\Db;

/**
 * Class GoodsLogic
 * @package app\admin\logic
 *
 * 库存产品录入
 * 产品出库入库
 * 库存统计
 */
class GoodsLogic
{
    /*
     * 新增产品
     */
    function addT($data)
    {
        (new Tgoods($data))->save();
    }

    /*
     * 修改信息
     */
    function modifyT($data, $num)
    {
        (new Tgoods())->save($data, $num);
    }

    /*
     * 删除产品
     */
    function delT($num)
    {
        Tgoods::destroy($num);
    }

    /*
     * 产品编号模糊查询
     */
    function queryByNum($num)
    {
        return (new Tgoods())->where(["num" => ["like", $num . "%"]])->order("num")->select();
    }

    /*
     * 所有商品种类
     */
    function allT()
    {
        return (new Tgoods())->order("num")->select();
    }

    /*
     * 删除出入库记录
     */
    function delLog($id)
    {
        return Glogs::destroy($id);
    }

    /*
     * 库存查询
     */
    function query($branch_id)
    {
        $sql = "SELECT
                    t.num,
                    t.`name`,
                    g.branch_id,
                    t.price,
                    g.`storage`
                FROM
                    goods g
                INNER JOIN tgoods t ON t.num = g.num
                WHERE
                    branch_id = $branch_id
                ORDER BY
                    t.num";
        return DB::query($sql);
    }

    function queryJqGrid($where = '', $page = 1, $rows = 20, $q = '', $sidx = '', $sord = '')
    {
        $good = model('goods');
        $param = json_decode($q, true);
        $conditions = [];
        $orders = '';
        if (!empty($param['num'])) {
            $num = $param['num'];
            $conditions['num'] = array('like', "%$num%");
        }
        if (!empty($param['name'])) {
            $name = $param['name'];
            $conditions['name'] = array('like', "%$name%");
        }
        if (!empty($param['price'])) {
            $price = $param['price'];
            $conditions['price'] = array('like', "%$price%");
        }
        if (!empty($sidx)) {
            $orders = " $sidx $sord ";
        }
        $pageInfo = $good->where($where)->where($conditions)->order($orders)->paginate($rows);
        //p($pageInfo);
        $data['total'] = $pageInfo->lastPage();
        $data['page'] = $pageInfo->currentPage();
        $data['rows'] = $pageInfo->items();

        return $data;
    }

    function queryJqGridLog($where = '', $page = 1, $rows = 20, $q = '', $sidx = '', $sord = '')
    {
        $good = model('Glogs');
        $param = json_decode($q, true);
        $conditions = [];
        $orders = '';
        if (!empty($param['goods_num'])) {
            $goods_num = $param['goods_num'];
            $conditions['goods_num'] = array('like', "%$goods_num%");
        }
        if (!empty($param['gname'])) {
            $gname = $param['gname'];
            $conditions['gname'] = array('like', "%$gname%");
        }
        if (!empty($param['price'])) {
            $price = $param['price'];
            $conditions['price'] = array('like', "%$price%");
        }
        if (!empty($sidx)) {
            $orders = " $sidx $sord ";
        } else {
            $orders = " date desc ";
        }
        $pageInfo = $good->with('good')->where($where)->where($conditions)->order($orders)->paginate($rows);
        //p($pageInfo);
        $data['total'] = $pageInfo->lastPage();
        $data['page'] = $pageInfo->currentPage();
        $data['rows'] = $pageInfo->items();

        return $data;
    }

    /*
     * 产品出库入库
     */
    function pop_push($goods_num, $branch_id, $count, $user_id)
    {
        $goods = Goods::get(["num" => $goods_num, "branch_id" => $branch_id]);
        if ($goods) {
            $goods["storage"] += $count;
        } else {
            $goods = new Goods(["num" => $goods_num, "branch_id" => $branch_id, "storage" => $count]);
        }
        $type = $count > 0 ? "入库" : "出库";
        $glog = new Glogs(["goods_num" => $goods_num, "type" => $type, "branch_id" => $branch_id, "count" => $count, "user_id" => $user_id]);
        $glog->save();
        $goods->save();
        return "success";
    }

    /*
     * 出入库查询
     */
    function logList($where)
    {
        $where = $where ? $where : "1 = 1";
        $sql = "SELECT
                    g.id,
                    t.num,
                    t.`name`,
                    g.branch_id,
                    g.count,
                    u.`name` user_name,
                    g.date
                FROM
                    glogs g
                INNER JOIN tgoods t ON t.num = g.goods_num
                INNER JOIN `user` u ON u.id = g.user_id
                WHERE
                    $where
                ORDER BY
	                g.date";
        return DB::query($sql);
    }

    function add($data)
    {
        // 添加入库记录
        $data_ruku['goods_num'] = $data['num'];
        $data_ruku['count'] = $data['storage'];
        $data_ruku['user_id'] = session('id');
        $data_ruku['type'] = '入库';
        model('Glogs')->data($data_ruku)->allowField(true)->isUpdate(false)->save();
        if (model('Goods')->where('num', $data['num'])->count() > 0) {// 有相应编号的产品
            return model('Goods')->where('num', $data['num'])->setInc('storage', $data['storage']);
        } else {
            return model('Goods')->data($data)->allowField(true)->isUpdate(false)->save();
        }

    }

    // 删除数据
    public function del($num)
    {
        return model('Goods')->where(["num" => $num])->delete();
    }
}